package com.lxyk.yygh.msm.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.google.gson.Gson;
import com.lxyk.yygh.msm.service.MsmService;
import com.lxyk.yygh.msm.utils.MsmUtils;
import com.lxyk.yygh.vo.msm.MsmVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.Map;

@Service
@Slf4j
public class MsmServiceImpl implements MsmService {


    //发送手机验证码
    @Override
    public boolean send(String phone, String code) {
        if(StringUtils.isEmpty(phone)){
           return false;
        }

        //整合阿里云短信服务
        //设置相关参数
        DefaultProfile profile = DefaultProfile.getProfile(MsmUtils.REGION_Id, MsmUtils.ACCESS_KEY_ID, MsmUtils.ACCESS_KEY_SECRECT);
        IAcsClient client = new DefaultAcsClient(profile);
        SendSmsRequest request = new SendSmsRequest();
        request.setPhoneNumbers(phone);//接收短信的手机号
        request.setSignName("预约挂号网站");//短信签名
        request.setTemplateCode("SMS_462265090");//短信模板code
        //设置模板对应的值
        //验证码使用json的格式 {"code":"123456"}
        Map<String,Object> param=new HashMap<>();
        param.put("code",code);
        request.setTemplateParam(JSONObject.toJSONString(param));
        log.info("client===>{}",client);
        //调用方法进行短信发送
        try {
//            log.info("client===>{}",client);
            SendSmsResponse response = client.getAcsResponse(request);
            System.out.println("*************短信发送返回结果"+new Gson().toJson(response));
            return true;
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            System.out.println("ErrCode:" + e.getErrCode());
            System.out.println("ErrMsg:" + e.getErrMsg());
            System.out.println("RequestId:" + e.getRequestId());
        }
        return false;
    }

    //MQ发送短信
    @Override
    public boolean send(MsmVo msmVo) {
        //手机号不为空才发送短信
        if(!StringUtils.isEmpty(msmVo.getPhone())){
//           String code= (String)msmVo.getParam().get("code");
//            改造
//            boolean isSend = this.send(msmVo.getPhone(), code);
            System.out.println("--------rabbitmq--msmVo模块--------");
            System.out.println();
            System.out.println("短信模板code--"+msmVo.getTemplateCode());
            System.out.println("短信模板参数--"+msmVo.getParam());
            System.out.println("短信模板电话--"+msmVo.getPhone());
            boolean isSend = this.send(msmVo.getPhone(), msmVo.getParam());
            return isSend;
        }
        return false;
    }


    //param里面有code
    private boolean send(String phone,Map<String,Object> param){
        //判断手机号是否为空
        if(StringUtils.isEmpty(phone)) {
            return false;
        }

        //整合阿里云短信服务
        //设置相关参数
        DefaultProfile profile = DefaultProfile.getProfile(MsmUtils.REGION_Id, MsmUtils.ACCESS_KEY_ID, MsmUtils.ACCESS_KEY_SECRECT);
        IAcsClient client = new DefaultAcsClient(profile);
        SendSmsRequest request = new SendSmsRequest();

        request.setPhoneNumbers(phone);//接收短信的手机号
        request.setSignName("预约挂号网站");//短信签名
        request.setTemplateCode("SMS_462265090");//短信模板code
        //设置模板对应的值
        request.setTemplateParam(JSONObject.toJSONString(param));
        //调用方法进行短信发送
        try {
//            log.info("client===>{}",client);
            SendSmsResponse response = client.getAcsResponse(request);
            System.out.println("*************短信发送返回结果"+new Gson().toJson(response));
            return true;
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            System.out.println("ErrCode:" + e.getErrCode());
            System.out.println("ErrMsg:" + e.getErrMsg());
            System.out.println("RequestId:" + e.getRequestId());
        }

        return false;
    }
}
